package com.example.chapter07_server.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.example.chapter07_server.database.UserDBHelper;

public class UserInfoProvider extends ContentProvider {

    private final String TAG = "liu";

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int USERS = 1;
    private static final int USER = 2;

    static {
       URI_MATCHER.addURI(UserInfoContent.AUTHORITIES,"/user",USERS);
       URI_MATCHER.addURI(UserInfoContent.AUTHORITIES,"/user/#",USER);
    }
    private UserDBHelper dbHelper;

    @Override
    public boolean onCreate() {
        Log.d(TAG, "UserInfoProvider onCreate");
        dbHelper = UserDBHelper.getInstance(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        if(URI_MATCHER.match(uri) == USERS){
            Log.d(TAG, "userInfoProvider query");
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            Log.d(TAG, db == null ? "数据库未拿到" : "数据库通过dbHelper拿到");
            if (db == null) {
                throw new RuntimeException("db is null");
            }
            Cursor cursor = db.query(UserDBHelper.TABLE_NAME, projection, selection, selectionArgs,
                    null, null,
                    sortOrder);
            Log.d(TAG, cursor == null ? "游标未拿到" : "游标已拿到");
            return cursor;
        }
        return null;

    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if(URI_MATCHER.match(uri) == USERS){
            Log.d(TAG, "UserInfoProvider insert");
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Log.d(TAG, db == null ? "数据库未拿到" : "数据库通过dbHelper拿到");
            if (db == null) {
                throw new RuntimeException("db is null");
            }

            long rowId = db.insert(UserDBHelper.TABLE_NAME, null, values);
            //通知别人我的数据发生改变
            /*
            *if(rowId > 0){//判断插入是否执行成功
                //添加成功，就利用新纪录的行号生成新的地址
                Uri newUri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI,rowId);
                //通知监听器，数据已经改变
                getContext().getContentResolver().notifyChange(newUri,null);
            }*/
        }


        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;
        /*URI_MATCHER.match(uri) 返回的是code值*/
        switch (URI_MATCHER.match(uri)){
            // content://com.example.chapter07_server.provider.UserInfoProvider/user
            case USERS:
                //删除多行
                Log.d(TAG, "UserinfoProvider delete");
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Log.d(TAG, db == null ? "db is null" : "db is not null");
                if(db == null){
                    throw new RuntimeException("db is null");
                }
                count = db.delete(UserDBHelper.TABLE_NAME,selection,selectionArgs);
                break;

            //content://com.example.chapter07_server.provider.UserInfoProvider/user/2
            //删除单行
            case USER:
                String id = uri.getLastPathSegment();
                SQLiteDatabase db1 = dbHelper.getWritableDatabase();
                count = db1.delete(UserDBHelper.TABLE_NAME,"_id=?",new String[]{id});
                break;
        }

        return count;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}